﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Net.Mime;
using DAL;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Web.UI;
using System.Globalization;



public partial class Report_UserControl_uc_YeuCauSoanHangReport : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            InitData();
        }
    }

    private void InitData()
    {
        lblAlerting.Text = string.Empty;
        txtNgay.Text = DateTime.Now.ToString("dd/MM/yyyy");
        LoadDataComboboxRoute();
        LoadDataCombobox();
        hdfDateTime.Value = "";
    }
    private void LoadDataComboboxRoute()
    {
        List<ZD_PRC_ZD_CATE_ROUTE_GETALLResult> lst = new List<ZD_PRC_ZD_CATE_ROUTE_GETALLResult>();
        try
        {
            CateRouteBO ct = new CateRouteBO();
            lst = ct.CateRouteGetAll();
            if (lst != null)
            {
                ddlRoute.DataSource = lst;
                ddlRoute.DataTextField = "ROUTENAME";
                ddlRoute.DataValueField = "ROUTEID";
                ddlRoute.DataBind();
            }
            foreach (RadComboBoxItem item in ddlRoute.Items)
            {
                item.Checked = true;
            }
        }
        catch (Exception)
        {


        }
    }
    private void LoadDataCombobox()
    {
        List<ZD_PRC_OCRG_GETALLResult> result = new List<ZD_PRC_OCRG_GETALLResult>();
        try
        {
            DeliveryBO del = new DeliveryBO();
            result = del.GroupCustomerGetAll();
            ddlGroupCode.DataSource = result;
            ddlGroupCode.DataTextField = "GroupName";
            ddlGroupCode.DataValueField = "GroupCode";
            ddlGroupCode.DataBind();
            foreach (RadComboBoxItem item in ddlGroupCode.Items)
            {
                item.Checked = true;
            }
        }
        catch (Exception)
        {


        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        lblAlerting.Text = string.Empty;
        DateTime dt = new DateTime();
        try
        {
            dt = DateTime.ParseExact(txtNgay.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture);
            hdfDateTime.Value = txtNgay.Text.Trim();
        }
        catch
        {
            hdfDateTime.Value = "";
            lblAlerting.Text = "Bạn chưa nhập ngày hoặc ngày không đúng!";
            return;
        }
        if (ddlRoute.CheckedItems.Count <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn tuyến!";
            return;
        }
        if (ddlGroupCode.CheckedItems.Count <= 0)
        {
            lblAlerting.Text = "Bạn chưa chọn nhóm khách hàng!";
            return;
        }        
        // kiem tra xem la Ngay hom nay va buoi hom nay da duoc lap phieu xuat chua?

        DeliveryBO del = new DeliveryBO();
        List<ZD_PRC_ZD_TONG_HOP_THEO_ROUTEResult> result = new List<ZD_PRC_ZD_TONG_HOP_THEO_ROUTEResult>();
        result = del.GetDeliveryOfRoute(dt, GetItemOfDDL(ddlRoute), GetItemOfDDL(ddlGroupCode));
        repRouteList.DataSource = result;
        repRouteList.DataBind();
        GetDeliveryDetail(dt, GetItemOfDDL(ddlGroupCode));
    }
    private void GetDeliveryDetail(DateTime dt, string DSGroupCode)
    {
        DeliveryBO del = new DeliveryBO();
        foreach (RepeaterItem item in repRouteList.Items)
        {
            string ROUTERID = ((HiddenField)item.FindControl("hdfRouteID")).Value;
            List<ZD_PRC_ZD_TONG_HOP_THEO_ROUTEID_REPORTResult> result = new List<ZD_PRC_ZD_TONG_HOP_THEO_ROUTEID_REPORTResult>();
            result = del.GetDeliveryByRouteID(dt, int.Parse(ROUTERID), DSGroupCode);

            Repeater repRouteDetail = (Repeater)item.FindControl("repRouteDetail");
            repRouteDetail.DataSource = result;
            repRouteDetail.DataBind();
        }

    }


    private string GetItemOfDDL(RadComboBox rbb)
    {
        string result = "";
        if (rbb.CheckedItems.Count > 0)
        {
            foreach (RadComboBoxItem item in rbb.CheckedItems)
            {
                result += item.Value + "@";
            }

        }
        return result;
    }

}
